package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.Schedule;
import com.woniuxy.factory.entity.WorkOrder;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.WorkOrderMapper;
import com.woniuxy.factory.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkOrderServiceImp implements WorkOrderService {

    @Autowired(required = false)
    WorkOrderMapper wom;
    //分页查询所有的派单信息
    @Override
    public List<WorkOrder> selectWorkOrders(WorkOrder workOrder,int page,int limit) {
        //设置起始页
        page = (page-1)*limit;
        List<WorkOrder> lwo = wom.selectWorkOrders(workOrder,page,limit);
        return lwo;
    }

    //查询总数目
    @Override
    public int selectWorkOrderCount(WorkOrder workOrder) {
        int count  = wom.selectWorkOrderCount(workOrder);
        return count;
    }

    //新增派单
    @Override
    public void insertWorkOrder(WorkOrder workOrder) {
        //插入数据
        wom.insertWorkOrder(workOrder);
    }


    //删除
    @Override
    public void deleteWorkOrderById(String worId) {
        wom.deleteWorkOrderById(worId);
    }

    //数据回显查询当前编号的数据
    @Override
    public WorkOrder workOrderEchoById(String worId) {
        WorkOrder workOrder = wom.workOrderEchoById(worId);
        return workOrder;
    }

    //修改
    @Override
    public void updateWorkOrderById(WorkOrder workOrder) {

        wom.updateWorkOrderById(workOrder);
    }

    //删除选中
    @Override
    public void deleteWorkOrderSelected(int[] worIds) {
        wom.deleteWorkOrderSelected(worIds);
    }

    @Override
    public void selectWorkOrderById(String planid) {
        //先判断当前计划单是否已经安排
        WorkOrder wo = wom.selectWorkOrderByPlanid(planid);
        if(wo!=null){
            throw new BusinessException("已派单，不可重复提交");
        }
    }

    //查询全部的派单信息
    @Override
    public List<WorkOrder> selectAllWorkOrder() {
        List<WorkOrder> lwo = wom.selectAllWorkOrder();
        return lwo;
    }

    //修改之前先判断当前计划单是否已经投入生产，已生成就不能修改
    @Override
    public void selectScheduleByplanId(String planid) {
        Schedule s = wom.selectScheduleByplanId(planid);
        if(s!=null){
            throw new BusinessException("该计划单已投入生产，不能修改");
        }
    }

    //申请添加回显派单信息
    @Override
    public WorkOrder selectWorkOrderEchoById(String planid) {
        WorkOrder wo = wom.selectWorkOrderEchoById(planid);
        return wo;
    }
}
